<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cart extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
        $this->load->model("product_model");
        $this->load->model("cat_model");
        
        // Category List
        $query_cat = $this->cat_model->getFirstPageData();
        
        //--- List Of Category
        $this->data['query_cat'] = $query_cat;  
    }
    
    public function index(){
        log_message('debug', get_class($this).' //Call index');
        
        $action = $this->input->post('action');
        $PRD_ID = $this->input->post('PRD_ID');
        $QTY = $this->input->post('QTY');
        
        $member_id = 0;
        
        if($action == CART_AC_ADDCART && $PRD_ID && $QTY){     
            log_message('debug', get_class($this).' // Add Cart.');
            $sale_price = $this->product_model->getSalePriceById($PRD_ID);       
            $this->cart_model->addCart(session_id(), $PRD_ID, $sale_price, $member_id, $QTY);
        } else if($action == CART_AC_DELCART && $PRD_ID) {
            log_message('debug', get_class($this).' // Delete Cart.');
            
            $this->cart_model->deleteCart(session_id(), $PRD_ID);
            return;
        } else if($action == CART_AC_UPDATECART) {
            log_message('debug', get_class($this).' // Update Cart.');
            $MAP = $this->input->post('MAP');
            
            $this->cart_model->updateAllPrdCart($MAP);
            return;
        }
        
        $this->callView();
    }
    
    private function callView(){
        $this->data['query'] = $this->cart_model->getCart(session_id());
        
        $this->load->view('cart/cart_view', $this->data);
    }
}

?>
